<?php
    $row = file_get_contents('php://input');

    if($row){
        $data = json_decode($row);
        $data->emails = explode(";",$data->emails);//群发
        require_once "Smtp.class.php";
        if($data){
            if($data->head === 20000){
                $len = count($data->emails);//获取数组长度
                for($i=0; $i<$len; $i++){
                    to_mail($i,$data);
                }
            }else{
                echo "发送数据库订阅者";
            };
        }else{
            // echo "对不起，邮件发送失败！请检查邮箱填写是否有误。";
            var_dump($data);
            exit();
        };
    }else{
        echo "没有获取到邮箱信息。";
    };

    function to_mail($i,$obj){
        $row = file_get_contents('php://input');
        include '../../api/config.php';
        if($row){
            $data = json_decode($row);
            $token = $data->token;
            $sql_1 = "SELECT * FROM admin_user WHERE token='$token'";
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $result = $stmt->fetchAll();

            if($result[0]['name'] != ""){
                $sql = "SELECT * FROM sys_email";
                $stmt = $conn->query($sql);
                $stmt->execute();
                $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                $result = $stmt->fetchAll();
                $emails_data = $result;

                //******************** 配置信息 ********************************
                $smtpserverport = $emails_data[0]['SMTP_port'];//SMTP服务器端口普通：25 加密：465
                if($smtpserverport == '25'){
                    $smtpserver = $emails_data[0]['SMTP_name'];//SMTP服务器
                }else{
                    $smtpserver = 'ssl://'.$emails_data[0]['SMTP_name'];//SMTP服务器
                };
                $smtpusermail = $emails_data[0]['email'];//SMTP服务器的用户邮箱
                $smtpemailto = $obj->emails[$i];//发送给谁
                $smtpuser = $emails_data[0]['email'];//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
                $smtppass = $emails_data[0]['email_pos'];//SMTP服务器的授权码
                $mailtitle = $obj->titles;//邮件主题
                $mailcontent = $obj->conts;//邮件内容
                $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
                //************************ 配置信息 ****************************
                $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
                $smtp->debug = false;//是否显示发送的调试信息
                $state = $smtp->sendmail($smtpemailto, $smtpusermail,$emails_data[0]['email_name'], $mailtitle, $mailcontent, $mailtype);
                $conn = null;
                if($state==""){
                    echo "对不起，邮件发送失败！请检查邮箱填写是否有误。";
                    exit();
                }else{
                    echo "发送到邮箱：【 ".$obj->emails[$i]." 】成功!";
                }

            }else{
                    echo "抱歉，权限不足";
            };
        };

        
    };
?>